package com.example.administrator.appstpremvpdemo.common;

import android.app.Application;
import android.graphics.Bitmap;
import android.os.SystemClock;
import android.util.Log;

import com.example.administrator.appstpremvpdemo.helper.CauchExceptionHandler;
import com.example.administrator.appstpremvpdemo.helper.HandleSSLHandshakeHelper;
import com.example.administrator.appstpremvpdemo.helper.SplashResBlockHelper;
import com.example.administrator.appstpremvpdemo.helper.ThreadPoolHelper;
import com.example.administrator.appstpremvpdemo.util.CrashUtils;
import com.example.administrator.appstpremvpdemo.view.fragment.LaunchDefaultFragment;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.cache.DiskCacheStore;
import com.yanzhenjie.nohttp.rest.RequestQueue;


/**
 * Created by Administrator on 2017/11/30.
 */

public class MainApplication extends Application {

    //请求队列
    private static RequestQueue mRequestQueue;

    public static RequestQueue getmRequestQueue() {
        return mRequestQueue;
    }

    public static MainApplication getInstance() {
        return sInstance;
    }

    private static MainApplication sInstance = null;

    public MainApplication() {
        sInstance = this;
    }

    private LaunchDefaultFragment.Entity mTempEntity;//临时中转的实体

    @Override
    public void onCreate() {
        super.onCreate();
        //这里我使用IntentService代替初始化工作。但是很多三方SDK只允许在application中初始化。那就按他的做。
        //new DownloadService().start(this);

        //NoHttp的初始化工作，
        NoHttp.initialize(this, new NoHttp.Config()
                // 设置全局连接超时时间，单位毫秒
                .setConnectTimeout(10 * 1000)
                // 设置全局服务器响应超时时间，单位毫秒
                .setReadTimeout(10 * 1000)
        );
        //配置缓存，控制开关
        NoHttp.initialize(this, new NoHttp.Config()
                .setCacheStore(new DiskCacheStore(this)));
        Log.w("DownloadService", "初始化ok");
        mRequestQueue = NoHttp.newRequestQueue();
        //处理https证书问题
        HandleSSLHandshakeHelper.handleSSLHandshake();
        //设置全局Crash处理类
        //CrashUtils.init("AppStoreCrashDir");
        //CauchExceptionHandler.getInstance().setDefaultUnCachExceptionHandler();

        ThreadPoolHelper.runOnThread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(2000);
            }
        });

        Log.w("MainApplication", "ThreadPoolHelper.sThreadPoolExecutor.getActiveCount():" + ThreadPoolHelper.sThreadPoolExecutor.getActiveCount());
    }

    /**
     * 开启异步线程去加载闪屏页缓存实体
     */
    public void asynLoadSplashEntity() {

        ThreadPoolHelper.runOnThread(new Runnable() {
            @Override
            public void run() {
                getSplashEntity();
            }
        });
    }

    private LaunchDefaultFragment.Entity getSplashEntity() {
        //TODO 加载闪屏页缓存实体
        SplashResBlockHelper splashResBlockHelper = new SplashResBlockHelper(MainApplication.getInstance());
        Bitmap bitmap = splashResBlockHelper.loadSplashPageRes();
        boolean isSystem = false;
        return LaunchDefaultFragment.Entity.createEntity(bitmap, isSystem, splashResBlockHelper);
    }

    /**
     * 获取闪屏数据,先判断是否已经拿到缓存数据
     */
    public LaunchDefaultFragment.Entity requsetSplashData() {
        //TODO 联网获取最新闪屏数据并缓存,并设置给LaunchDefaultFragment.Entity实体，方便LaunchDefaultFragmentn能拿到数据
        //case 图片数据拿到，就直接赋值给实体entity
        //case 图片数据没有拿到，就调用getSplashEntity方法去获取；
        if (mTempEntity != null) {
            LaunchDefaultFragment.Entity splashEntity = mTempEntity;
            mTempEntity = null;
            return splashEntity;
        }
        LaunchDefaultFragment.Entity splashEntity = getSplashEntity();
        mTempEntity = null;
        return splashEntity;
    }

}
